﻿module RPG {
    export class Player extends Phaser.Sprite {
        constructor(game: Phaser.Game, x: number, y: number, name: string) {
            super(game, x, y, name, 0);

            this.anchor.setTo(0.5, 0);

            this.animations.add('upward_walk', [4, 5, 6, 7], 10, true);
            this.animations.add('downward_walk', [0, 1, 2, 3], 10, true);
            this.animations.add('left_walk', [8, 9, 10, 11], 10, true);
            this.animations.add('right_walk', [12, 13, 14, 15], 10, true);

            this.game.physics.arcade.enableBody(this);
            this.body.allowGravity = false;
            this.body.immovable = true;

            this.body.collideWorldBounds = true;
        }

        update() {
            var speed = 50;

            this.body.velocity.x = 0;
            this.body.velocity.y = 0;

            if (this.game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
                this.body.velocity.x -= speed;
                this.animations.play('left_walk');
            } else if (this.game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
                this.body.velocity.x += speed;
                this.animations.play('right_walk');
            } else if (this.game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) {
                this.body.velocity.y += speed;
                this.animations.play('upward_walk');
            } else if (this.game.input.keyboard.isDown(Phaser.Keyboard.UP)) {
                this.body.velocity.y -= speed;
                this.animations.play('downward_walk');
            } else {
                this.animations.frame = 4;
            }

        }
    }
}